WorkerScript.onMessage = function(message) {
    doLoadSpots(message);
}


function doLoadSpots(message) {
    var xhr = new XMLHttpRequest;
    var url = "https://production-2.sogeoapi.com/api/poi?stats=true&per_page=" + message.g.perPage + "&oauth_token=" + message.token;
    if (message.geo != "NaN,NaN") {
        url += "&geo=" + message.geo;
    }
    xhr.open("GET", url);
    xhr.onreadystatechange = function() {
        try {
            if (xhr.readyState == XMLHttpRequest.DONE) {
                message.model.clear();
                var response = JSON.parse(xhr.responseText);
                for (var i in response.data) {
                    var spot = response.data[i].poi;
                    message.model.append({
                                  spotName: spot.name,
                                  spotInCollection: spot.stats.own_book,
                                  spotId: spot.id,
                                  index: i,
                                  spotTags: spot.tag_list != null ? spot.tag_list : ""
                    });
                }
                message.model.sync();
                WorkerScript.sendMessage({ 'result': 'SUCCESS', 'listState': '' })
            }
        } catch(err) {
            WorkerScript.sendMessage({ 'result': 'FAILURE', 'message': err + ", " + xhr.responseText, 'listState': '' });
        }

    }
    xhr.send();
}
